//
//  Refreshable.swift
//  RxDemo
//
//  Created by MAC on 2019/5/28.
//  Copyright © 2019 zk. All rights reserved.
//

import Foundation
import MJRefresh


protocol Refreshable {
    
}

extension Refreshable where Self: UIViewController {
    
    func initRefreshGifHeader(_ scrollView: UIScrollView, _ action: @escaping () -> Void) -> MJRefreshHeader {
        
        let header = RefreshGifHeader { action() }
        let iamges = [UIImage(named: "pullToRefresh_0")!,
                      UIImage(named: "pullToRefresh_1")!,
                      UIImage(named: "pullToRefresh_2")!,
                      UIImage(named: "pullToRefresh_3")!,
                      UIImage(named: "pullToRefresh_4")!,
                      UIImage(named: "pullToRefresh_5")!,
                      UIImage(named: "pullToRefresh_6")!,
                      UIImage(named: "pullToRefresh_6")!,
                      UIImage(named: "pullToRefresh_7")!,
                      UIImage(named: "pullToRefresh_8")!,
                      UIImage(named: "pullToRefresh_9") as Any]
        
        header?.setTitle("分享付费专辑，还可以赚钱哦", for: .pulling)
        header?.lastUpdatedTimeLabel.isHidden = true
        header?.setImages(iamges, for: .pulling)
        scrollView.mj_header = header
        return scrollView.mj_header
    }
    
    func initRefreshHeader(_ scrollView: UIScrollView, _ action: @escaping () -> Void) -> MJRefreshHeader {
        
        scrollView.mj_header = MJRefreshNormalHeader(refreshingBlock: { action() })
        return scrollView.mj_header
    }
    
    func initRefreshFooter(_ scrollView: UIScrollView, _ action: @escaping () -> Void) -> MJRefreshFooter {
        
        scrollView.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: { action() })
        return scrollView.mj_footer
    }
}

extension Refreshable where Self: UIScrollView {
    
    func initRefreshGifHeader(_ scrollView: UIScrollView, _ action: @escaping () -> Void) -> MJRefreshHeader {
        
        let header = RefreshGifHeader { action() }
        let iamges = [UIImage(named: "pullToRefresh_0")!,
                      UIImage(named: "pullToRefresh_1")!,
                      UIImage(named: "pullToRefresh_2")!,
                      UIImage(named: "pullToRefresh_3")!,
                      UIImage(named: "pullToRefresh_4")!,
                      UIImage(named: "pullToRefresh_5")!,
                      UIImage(named: "pullToRefresh_6")!,
                      UIImage(named: "pullToRefresh_6")!,
                      UIImage(named: "pullToRefresh_7")!,
                      UIImage(named: "pullToRefresh_8")!,
                      UIImage(named: "pullToRefresh_9") as Any]
        
        header?.setTitle("分享付费专辑，还可以赚钱哦", for: .pulling)
        header?.lastUpdatedTimeLabel.isHidden = true
        header?.setImages(iamges, for: .pulling)
        scrollView.mj_header = header
        return scrollView.mj_header
    }
    
    func initRefreshHeader(_ scrollView: UIScrollView, _ action: @escaping () -> Void) -> MJRefreshHeader {
        
        scrollView.mj_header = MJRefreshNormalHeader(refreshingBlock: { action() })
        return scrollView.mj_header
    }
    
    func initRefreshFooter(_ scrollView: UIScrollView, _ action: @escaping () -> Void) -> MJRefreshFooter {
        
        scrollView.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: { action() })
        return scrollView.mj_footer
    }
}
